/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.variable.expressions;

import tal.drivechain.chain.ChainNode;
import tal.drivechain.expression.dcExpression;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.SetProperty.Flag;


/**
 *
 * @author Main
 */
public class SetPropertyExpression extends CustomExpression {
    String plugname;
    Flag flag;
    
    public SetPropertyExpression(String value, DataType returnDataType) {
        super(value, returnDataType);
    }

    @Override
    protected Object parseExpression(String value) {
        Object[] params = SetProperty.parseSetProperty(value);
        plugname = (String)params[0];
        String[] sprops = (String[])params[1];
        flag = (Flag)params[2];
        dcExpression[] expr = new dcExpression[sprops.length];
        for (int i=0; i<expr.length; i++) {
            expr[i] = new dcExpression(sprops[i], DataType.STRING);
        }
        return expr;
    }

    @Override
    public String toString() {
        String ret = "[prop]" + "[" + plugname + "/" + flag.getName() + "]{";
        for (dcExpression e : (dcExpression[])expression)
            ret += e.toString() + "; ";
        return ret.substring(0, ret.length()-2) + "}";
    }

    @Override
    public Object evaluate(ChainNode context) throws IllegalArgumentException {
        String props = "";
        dcExpression[] expr = (dcExpression[])expression;
        for (dcExpression e : expr) {
            props += e.evaluate(context) + ",";
        } props = props.substring(0, props.length()-1);
        return new SetProperty(plugname, props, flag);
    }

}
